package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.ywgt.pojo.User;
import com.ywgt.pojo.UserAccount;
import com.ywgt.service.AccountTraceService;
import com.ywgt.service.UserAccountService;
import com.ywgt.service.UserService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/user")
@ApiModel("用户管理")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    UserAccountService userAccountService;

    /**
     * page: 1,
     * limit: 20,
     * username: undefined,
     * mobile: undefined,
     * sort: 'add_time',
     * order: 'desc'
     * @return
     */
    @GetMapping("/list")
    public Object users(String username, String mobile, @RequestParam(required = false,defaultValue = "1") int page,
                        @RequestParam(required = false,defaultValue = "10") int limit,
                        @RequestParam(required = false,defaultValue = "add_time") String sort,
                        @RequestParam(required = false,defaultValue = "desc") String order){
        Page<User> users = userService.getUsers(username, mobile, page, limit, sort, order);
        List<User> userList = users.getResult();
        long total = users.getTotal();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", userList);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/detailApprove")
    public Object detail(@RequestParam(required = true) int id){
        UserAccount userAccount = userAccountService.getById(id);
        return  ResponseUtil.ok(userAccount);
    }
}
